package com.aruba.paletteapplication;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.ColorFilter;
import android.graphics.LinearGradient;
import android.graphics.Paint;
import android.graphics.PixelFormat;
import android.graphics.RectF;
import android.graphics.Shader;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.graphics.Palette;
import android.util.AttributeSet;
import android.widget.ImageView;
import android.widget.LinearLayout;

public class MyCardLinearLayout extends LinearLayout {
    private Bitmap bitmap;


    public MyCardLinearLayout(Context context) {
        this(context, null);
    }

    public MyCardLinearLayout(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public MyCardLinearLayout(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public void attachImage(ImageView imageView) {
        if (imageView.getDrawable() instanceof BitmapDrawable) {
            BitmapDrawable bitmapDrawable = (BitmapDrawable) imageView.getDrawable();
            bitmap = bitmapDrawable.getBitmap();
        }

        generateShader();
    }

    private void generateShader() {
        if (bitmap == null) {
            return;
        }

        Palette.from(bitmap).generate(new Palette.PaletteAsyncListener() {
            @Override
            public void onGenerated(@Nullable Palette palette) {
                //柔和而暗的颜色
                Palette.Swatch swatch = palette.getDarkMutedSwatch();
                if (swatch == null) {
                    for (Palette.Swatch swatchTemp : palette.getSwatches()) {
                        swatch = swatchTemp;
                        break;
                    }
                }
                //渐变颜色,由深色变浅色
                int colors[] = new int[]{swatch.getRgb(), blurColor(swatch.getRgb())};
                setBackground(new ShaderDrawable(colors));
                invalidate();
            }
        });
    }

    /**
     * 将颜色变浅
     *
     * @param rgb
     * @return
     */
    private int blurColor(int rgb) {
        //三原色，每个原色站8个bit
        int red = rgb >> 16 & 0xff;
        int green = rgb >> 8 & 0xff;
        int bule = rgb & 0xff;

        //#000000为黑色，#FFFFFF为白色，所以值越小，颜色越深,反之，颜色越浅
        float ratdio = 1.5f;
        red = (int) Math.min(255, red * ratdio);
        green = (int) Math.min(255, green * ratdio);
        bule = (int) Math.min(255, bule * ratdio);

        return Color.argb(255, red, green, bule);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

    }


    class ShaderDrawable extends Drawable {
        private int colors[];
        private Paint mPaint = new Paint();

        public ShaderDrawable(int[] colors) {
            this.colors = colors;
            mPaint.setAntiAlias(true);
        }

        @Override
        public void draw(@NonNull Canvas canvas) {
            //画背景
            if (colors != null) {
                RectF rectF = new RectF(0, 0, canvas.getWidth(), canvas.getHeight());

                LinearGradient linearGradient = new LinearGradient(rectF.left, 0, rectF.right, 0, colors, null, Shader.TileMode.CLAMP);
                mPaint.setShader(linearGradient);

                canvas.drawRect(rectF, mPaint);
            }
        }

        @Override
        public void setAlpha(int alpha) {

        }

        @Override
        public void setColorFilter(@Nullable ColorFilter colorFilter) {

        }

        /**
         * ~OPAQUE：便是完全不透明，遮盖在他下面的所有内容
         * ~TRANSPARENT：透明，完全不显示任何东西
         * ~TRANSLUCENT：只有绘制的地方才覆盖底下的内容。
         *
         * @return
         */
        @Override
        public int getOpacity() {
            return PixelFormat.OPAQUE;
        }
    }
}
